/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Grupo;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class GrupoFacade extends AbstractFacade<Grupo> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public GrupoFacade() {
        super(Grupo.class);
    }

    public List<Grupo> consultarGrupos(int idUsuario) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Grupo> listaGrupos = null;

        sql.append("SELECT DISTINCT g.* ");
        sql.append("FROM grupo g ");
        sql.append("INNER JOIN pertenece p ");
        sql.append("ON g.idGrupo = p.idGrupo AND p.idUsuario = ?");

        try {
            query = em.createNativeQuery(sql.toString(), Grupo.class);
            query.setParameter(1, idUsuario);
            
            listaGrupos = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_NoResultException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_IllegalStateException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_QueryTimeoutException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_TransactionRequiredException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_PessimisticLockException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_LockTimeoutException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_PersistenceException:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarGrupos_Exception:".concat(e.getMessage()));
            listaGrupos = new ArrayList<Grupo>();
        } finally {
            return listaGrupos;
        }
    }
}
